package com.magnificent.atlantis.server.database.message;

import com.magnificent.atlantis.common.entity.ServerMeeting;
import com.magnificent.atlantis.common.entity.messages.ServerAcceptMeetingMessage;
import com.magnificent.atlantis.common.entity.messages.ServerMessage;
import com.magnificent.atlantis.server.database.AtlantisDataSource;
import com.magnificent.commons.db.datasource.ObjectEntity;

/**
 * User: Babanin
 * Date: 06.05.2009
 * Time: 12:44:20
 */
public class AcceptMeetingMessageHandler extends MessageHandler {
    public ObjectEntity handleMessage(ServerMessage message) {
        ServerAcceptMeetingMessage acceptMessage = (ServerAcceptMeetingMessage) message;

        // Send accept to meeting moderator
        AtlantisDataSource.getInstance().persist(acceptMessage);

        // Analyze
        ServerMeeting meeting = AtlantisDataSource.getInstance().getMeeting(acceptMessage.getEventId());
        if (meeting.getInvitedUsers().contains(acceptMessage.getUserFromId())) {
            meeting.getAcceptedByUsers().add(acceptMessage.getUserFromId());
            AtlantisDataSource.getInstance().update(meeting);
        }

        return message;
    }
}
